<?php
/**
 * 学年選択コンポーネント
 */
require_once APPLICATION_PATH . '/models/Players/Controller.php';

/**
 * 学年選択コンポーネントクラス
 * @link https://www.evernote.com/view/8b918e50-2ad1-4509-8d0d-da8a8d59e255?locale=ja#n=8b918e50-2ad1-4509-8d0d-da8a8d59e255
 */
class Components_SelectMember_Gakunen extends Components
{
	/**
	 * テンプレートファイル名
	 * application/views/Components/SelectMember/gakunen.tpl
	 */
	const TEMPLATE_NAME = 'gakunen.tpl';
	/** オンチェンジイベント起動メソッド名 */
	const ON_CHANGE_METHOD = "changeGakunen(this)";

	/** 初期選択バリュー */
	public $SELECTED;

	/** オンチェンジイベント起動メソッド名 */
	protected $_onChangeMethod;

	/** SELECT NAME */
	protected $_sName;
	/** OPTIONS */
	protected $_aryOptions;
	/** SELECTED */
	protected $_sSelected;
	/** ID */
	protected $_ID;
	/** 学校種別 */
	protected $_sSchoolType;

	/**
	 * コンストラクタ
	 * @param  $sCompileId
	 * @param  $sName
	 * @param  $aryOptions
	 * @param  $Id
	 */
	public function __construct($sCompileId, $sName, $aryOptions, $Id, $sSchoolType='')
	{
		parent::__construct(dirname(__FILE__), $sCompileId);

		/*** 学年変更イベントメソッド名設定 */
		$sOnMethod = self::ON_CHANGE_METHOD;
		$this->_onChangeMethod = $sOnMethod;

		$this->_ID = $Id;
		$this->_sName = $sName;
		$this->_sSchoolType = $sSchoolType;
		$this->_aryOptions = $aryOptions;
		if ( count($aryOptions) == 0 ) {
			$this->_setOptions();
		}
	}

	/**
	 * FETCH
	 * (non-PHPdoc)
	 * @see Smarty_Components_Select::fetch()
	 */
	public function fetch()
	{
		$this->view->SELECTED = $this->_getSelectedValue();
		$this->view->NAME = $this->_sName;
		$this->view->OPTIONS = $this->_aryOptions;
		$this->view->ID = $this->_ID;
		$this->view->ON_CHANGE = $this->_onChangeMethod;
		$sHtml = parent::fetch(self::TEMPLATE_NAME);
		return $sHtml;
	}

	/**
	 * HTTPリクエストから選択初期値を取得する
	 * Enter description here ...
	 */
	protected function _getSelectedValue()
	{
		$objRequest = new Lib_Request();
		$value = $objRequest->get($this->_sName);
		/*** 配列・文字列判定 */
		if (is_array($value)) {
			$value = $value[0];
		}
		return $value;
	}

	/**
	 * オプション設定
	 */
	protected function _setOptions()
	{
		switch ($this->_sSchoolType){
			case Models_Players_Controller::JUNIOR_SCHOOL:
				$aryOptions[$sName.'U7'] = '1年生';
				$aryOptions[$sName.'U8'] = '2年生';
				$aryOptions[$sName.'U9'] = '3年生';
				$aryOptions[$sName.'U10'] = '4年生';
				$aryOptions[$sName.'U11'] = '5年生';
				$aryOptions[$sName.'U12'] = '6年生';
				break;
			case Models_Players_Controller::MIDDLE_SCHOOL:
				$aryOptions[$sName.'U13'] = '1年生';
				$aryOptions[$sName.'U14'] = '2年生';
				$aryOptions[$sName.'U15'] = '3年生';
				break;
			case Models_Players_Controller::HIGH_SCHOOL:
				$aryOptions[$sName.'U16'] = '1年生';
				$aryOptions[$sName.'U17'] = '2年生';
				$aryOptions[$sName.'U18'] = '3年生';
				break;
		}
		$this->_aryOptions = $aryOptions;
	}
}